package event;

import java.util.ArrayList;
import java.util.Calendar;
import model.Model;
import warehouse.Resource;

public class Event extends Model{
	/**
	 * crea evento con id noto
	 * @param id
	 * @param eventName
	 * @param eventType
	 * @param preparationType
	 * @param beginDate
	 * @param place
	 * @param guestsNumber
	 * @param eventState
	 */
	public Event(long id, String eventName, EventType eventType, PreparationType preparationType, ServiceType serviceType ,Calendar beginDate,Calendar endDate, String place, int guestsNumber, EventState eventState) {
		super(id);
		this.eventType = eventType;
		this.preparationType = preparationType;
		this.beginDate = beginDate;
		this.endDate = endDate;
		this.place = place;
		this.guestsNumber = guestsNumber;
		this.eventName = eventName;
		this.serviceType = serviceType;
		this.eventState = eventState;
	}
	public Event(String eventName, EventType eventType, PreparationType preparationType, ServiceType serviceType, Calendar beginDate, Calendar endDate, String place, int guestsNumber, EventState eventState) {
		this(Model.NO_ID,eventName,eventType,preparationType, serviceType, beginDate,endDate, place,guestsNumber, eventState);
	}
	/**
	 * crea un nuovo evento
	 * @param eventName
	 * @param eventType
	 * @param preparationType
	 * @param beginDate
	 * @param place
	 * @param guestsNumber
	 * @param eventState
	 */
	public Event(String eventName, EventType eventType,PreparationType preparationType,ServiceType serviceType, Calendar beginDate,Calendar endDate, String place, int guestsNumber) {
		this(Model.NO_ID, eventName, eventType, preparationType, serviceType , beginDate, endDate,  place,guestsNumber, new AcceptanceState());
	}
	public Event(long id, String eventName, EventType eventType, PreparationType preparationType, ServiceType serviceType, Calendar beginDate, Calendar endDate, String place, int guestsNumber) {
		this(id,eventName,  eventType,  preparationType, serviceType, beginDate,endDate,  place, guestsNumber,new AcceptanceState());
	}
	public Event(String eventName, EventType eventType, PreparationType preparationType, ServiceType serviceType, Calendar beginDate, String place, int guestsNumber) {
		this(Model.NO_ID,eventName,  eventType,  preparationType, serviceType, beginDate, beginDate, place, guestsNumber,new AcceptanceState());
	}
	public EventType getEventType() {
		return eventType;
	}
	public void setEventType(EventType eventType) {
		this.eventType = eventType;
	}
	public PreparationType getPreparationType() {
		return preparationType;
	}
	public void setPreparationType(PreparationType preparationType) {
		this.preparationType = preparationType;
	}
	public ServiceType getServiceType() {
		return serviceType;
	}
	public void setServiceType(ServiceType serviceType) {
		this.serviceType = serviceType;
	}
	public Calendar getEndDate() {
		return endDate;
	}
	public void setEndDate(Calendar date){
		this.endDate = date;
	}
	public Calendar getBeginDate(){
		return this.beginDate;
	}
	public void setBeginDate(Calendar date) {
		this.beginDate = date;
	}
	public String getPlace() {
		return place;
	}
	public void setPlace(String place) {
		this.place = place;
	}
	public int getGuestsNumber() {
		return guestsNumber;
	}
	public void setGuestsNumber(int guestsNumber) {
		this.guestsNumber = guestsNumber;
	}
	public EventState getEventState(){
		return eventState;
	}
	public String getEventName() {
		return eventName;
	}
	public void setEventName(String eventName) {
		this.eventName = eventName;
	}

	//implementarlo nelle sottoclassi per seguire il pattern GRASP
	public String changeState(){
		if(eventState.getName().equalsIgnoreCase("ACCEPTANCE_STATE")){
			//reservedResources has to be initialized from db
			eventState = new PrePreparationState(guestsNumber, guestsNumber);
			return "PRE_PREPARATION_STATE";
		}
		if(eventState.getName().equalsIgnoreCase("PRE_PREPARATION_STATE")){
			//reservedResources has to be initialized from db
			eventState = new PreparationState();
			return "PROGRESS_STATE";
		}
		if(eventState.getName().equalsIgnoreCase("PROGRESS_STATE")){
			//reservedResources has to be initialized from db
			ArrayList<Resource> reservedResources = new ArrayList<Resource>();
			eventState = new ProgresState(reservedResources);
			return "PROGRESS_STATE";
		}
		if(eventState.getName().equalsIgnoreCase("PROGRESS_STATE")){
			eventState = new EndedState();
			return "ENDED_STATE";
		}
		return "ERROR";
	}
	
	public int getDays(){
		if(endDate == null)
			return 1;
		if(endDate.getTimeInMillis()== beginDate.getTimeInMillis())
			return 1;
		long diff = endDate.getTimeInMillis() - beginDate.getTimeInMillis();
		long sec = diff/1000;
		long hours = sec/360;
		long days = hours/24;
		return ((int)days/10);
	}
	
	public boolean contains(String filter) {
		String string = eventName +" "+ place +" "+eventType +" "+preparationType +" "+ serviceType + " "+ beginDate +" "+guestsNumber +" "+eventState.getName();
		if(string.contains(filter))
			return true;
		return false;
	}
	
	public String toString(){
		
		return "Nome: "+ eventName +
				"\nNumero di invitati: " +guestsNumber +
				"\nLuogo: "+ place +
				"\nDataInizio: "+ beginDate + 
				"\nDataFine: "+ endDate + 
				"\nTipoDiEvento: " +eventType +
				"\nPreparazione: " +preparationType ;
	}
	public enum EventType{WEDDING, BIRTHDAY,ANNIVERSARY, GRADUATION, SACRAMENT, MEETING, OTHER_PARTY}
	public enum PreparationType{STANDARD,ELITE,LUXURY}
	public enum ServiceType {BUFFET,TABLE}
	private EventType eventType;
	private PreparationType preparationType;
	private ServiceType serviceType;
	private Calendar beginDate;
	private Calendar endDate;
	private String place;
	private String eventName;
	private int guestsNumber;
	private EventState eventState;

}
